/*
 * LinkOptionsPanel.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui;

import javax.swing.event.PopupMenuEvent;
import org.fpdev.util.gui.GUIFactory;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.PopupMenuListener;
import org.fpdev.core.basenet.LinkTypes;

public class LinkOptionsPanel extends JPanel {

  private JTextField linkName_;
  private JCheckBox confirmLength_,  isWalkable_,  isOneWay_;
  private JComboBox type_;
  private JButton impNameBtn_;

  public LinkOptionsPanel() {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    linkName_ = new JTextField(18);
    impNameBtn_ = new JButton("Import");

    confirmLength_ = new JCheckBox("Confirm Length");

    /*type_ = new JComboBox();
    type_.addItem("Street");
    type_.addItem("Bike/Ped Only");
    type_.addItem("Heavy Rail");*/

    type_ = this.createLinkTypeComboBox();

    isWalkable_ = new JCheckBox("Is Walkable", true);
    isOneWay_ = new JCheckBox("One-way Street", false);

    type_.addItemListener(new ItemListener() {

      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == e.SELECTED) {
          switch (type_.getSelectedIndex()) {
            case 0:
              isWalkable_.setVisible(true);
              isOneWay_.setVisible(true);
              break;
            default:
              isWalkable_.setVisible(false);
              isOneWay_.setVisible(false);
          }
        }
      }
    });

    JPanel namePanel = new JPanel(new BorderLayout());
    JPanel nameSubPanel1 = new JPanel();
    nameSubPanel1.setLayout(new BoxLayout(nameSubPanel1, BoxLayout.Y_AXIS));
    nameSubPanel1.add(new JLabel("Link Name:"));
    nameSubPanel1.add(linkName_);
    JPanel nameSubPanel2 = new JPanel();
    nameSubPanel2.setLayout(new BoxLayout(nameSubPanel2, BoxLayout.Y_AXIS));
    nameSubPanel2.add(Box.createVerticalGlue());
    nameSubPanel2.add(impNameBtn_);
    namePanel.add(nameSubPanel1, BorderLayout.CENTER);
    namePanel.add(nameSubPanel2, BorderLayout.EAST);
    namePanel.setAlignmentX(LEFT_ALIGNMENT);
    add(namePanel);
    add(Box.createVerticalStrut(5));
    add(confirmLength_);
    add(Box.createVerticalStrut(5));
    //add(new JSeparator());
    add(new JLabel("Select Type:"));
    type_.setAlignmentX(LEFT_ALIGNMENT);
    add(type_);
    add(isWalkable_);
    add(isOneWay_);

    setBorder(new CompoundBorder(new TitledBorder("Link Options"), new EmptyBorder(2, 4, 2, 4)));//new EmptyBorder(8,8,8,8));
  }

  public static JComboBox createLinkTypeComboBox() {
    return createLinkTypeComboBox(180);
  }
  
  public static JComboBox createLinkTypeComboBox(final int width) {
    JComboBox cb = GUIFactory.newComboBox(width);

    // [10-19: Major Throroughfares, Open to Vehicular Traffic]

    cb.addItem(LinkTypes.MAJOR_TWOWAY + ". Major thoroughfare");
    /*11: Major thoroughfare w/ barrier-separated dedicated LRT/streetcar facility
    12: Major thoroughfare w/ barrier-separated dedicated bus/trolleybus lanes
    13: Major thoroughfare w/ non-barrier-separated dedicated LRT/streetcar facility
    14: Major thoroughfare w/ non-barrier-separated dedicated bus/trolleybus lanes
    15: Major thoroughfare w/ mixed-traffic LRT/streetcar facility
    16: Major thoroughfare w/ mixed-traffic trolleybus facility*/

    // [20-29: Minor Throroughfares, Open to Vehicular Traffic]

    cb.addItem(LinkTypes.MINOR_TWOWAY + ". Minor thoroughfare");
    /*21: Minor thoroughfare w/ barrier-separated dedicated LRT/streetcar facility
    22: Minor thoroughfare w/ barrier-separated dedicated bus/trolleybus lanes
    23: Minor thoroughfare w/ non-barrier-separated dedicated LRT/streetcar facility
    24: Minor thoroughfare w/ non-barrier-separated dedicated bus/trolleybus lanes
    25: Minor thoroughfare w/ mixed-traffic LRT/streetcar facility
    26: Minor thoroughfare w/ mixed-traffic trolleybus facility*/

    // [30-39: Other Walkable Streets, Open to Vehicular Traffic]

    cb.addItem(LinkTypes.LOCAL_TWOWAY + ". Local street");
    /*31: Public alleyway
    32: Walkable private circulation drive*/

    // [40-49: Non-Walkable Streets]

    cb.addItem(LinkTypes.NW_GENERAL + ". General non-walkable");
    cb.addItem(LinkTypes.NW_EXPWY_GENERAL + ". General expressway (one-way)");
    //41: Interstate / primary expressway (general traffic lanes)
    //42: Interstate / primary expressway (HOV/toll/managed lanes)
    //43: Secondary expressway / parkway
    cb.addItem(LinkTypes.NW_EXPWY_RAMP + ". Expressway ramp (one-way)");
    //45: Non-walkable surface street

    // [50-59: Bike/ped facilities closed to vehicular traffic]

    cb.addItem(LinkTypes.BP_GENERAL + ". General bike/ped facility");
    /*51: Pedestrian-only walkway (not bikeable)
    52: Pedestrianized street / promenade
    53: Multi-use trail
    54: Dedicated bikeway (no pedestrians)*/

    // [60-69: Dedicated off-street urban transit facilities] (see also 11-14, 21-24)

    cb.addItem(LinkTypes.TR_METRO + ". Fully grade-separated metro");
    cb.addItem(LinkTypes.TR_LRT + ". Off-street light rail");

    // [0-79: Railroad corridors]

    cb.addItem(LinkTypes.RR_STANDARD + ". Standard-gauge railroad");

    
    cb.addPopupMenuListener(new PopupMenuListener() {

      //Popup state to prevent feedback
      boolean stateCmb = false;
 
      //Extend JComboBox's length and reset it
      public void popupMenuWillBecomeVisible(PopupMenuEvent e)
      {
        JComboBox cb = (JComboBox) e.getSource();
        //Extend JComboBox

        int textWidth = width;
        for (int i = 0; i < cb.getItemCount(); i++) {
          String item = cb.getItemAt(i).toString();
          int w = cb.getGraphics().getFontMetrics().stringWidth(item);
          if(w > textWidth) textWidth = w; 
        }
        
        cb.setSize(textWidth+25, cb.getHeight());
        //If it pops up now JPopupMenu will still be short
        //Fire popupMenuCanceled...
        if(!stateCmb) cb.firePopupMenuCanceled();
        //Reset JComboBox and state
        stateCmb = false;
        cb.setSize(width, cb.getHeight());
      }

      //Show extended JPopupMenu
      public void popupMenuCanceled(PopupMenuEvent e) {
        JComboBox cmb = (JComboBox) e.getSource();
        stateCmb = true;
        //JPopupMenu is long now, so repop
        cmb.showPopup();
      }

      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 
        stateCmb = false;
      }
      
    });
    return cb;
  }

  public static JComboBox createBikeProvisionsComboBox() {
    JComboBox cb = GUIFactory.newComboBox(180);

    cb.addItem("0. (None)");
    cb.addItem(LinkTypes.BPPROV_BARRIER_SEPARATED_MULTIUSE + ". Barrier-separated Multi-Use Trail");
    cb.addItem(LinkTypes.BPPROV_STRIPED_LANES_FULL + ". Striped Bicycle Lanes");
    cb.addItem(LinkTypes.BPPROV_BIKEABLE_SHOULDER + ". Bikeable Shoulder");

    return cb;
  }

public static JComboBox createTransitProvisionsComboBox() {
    JComboBox cb = GUIFactory.newComboBox(180);

    cb.addItem("0. (None)");
    cb.addItem(LinkTypes.TRPROV_BARRIER_SEPARATED_LRT + ". Barrier-separated LRT");
    cb.addItem(LinkTypes.TRPROV_BARRIER_SEPARATED_BUS_LANES + ". Barrier-separated Bus-Only Lanes");
    cb.addItem(LinkTypes.TRPROV_INTEGRATED_LRT + ". In-Street LRT/Streetcar");
    cb.addItem(LinkTypes.TRPROV_INTEGRATED_BUS_LANES + ". Striped Bus Lanes");

    return cb;
  }

  public int getSelectedType() {
    /*switch(type_.getSelectedIndex()) {
    case 0: return CLink.CLASS_STREET;
    case 1: return CLink.CLASS_BIKEPED;
    case 2: return CLink.CLASS_HEAVYRAIL;
    //case 3: return CLink.STATION;			
    }
    return 0;*/
    return Integer.parseInt(((String) type_.getSelectedItem()).split("\\.")[0]);
  }

  public String getName() {
    return linkName_.getText();
  }

  public boolean getIsWalkable() {
    return isWalkable_.isSelected();
  }

  public boolean getIsOneWay() {
    return isOneWay_.isSelected();
  }

  public boolean getConfirmLength() {
    return confirmLength_.isSelected();
  }
}























